package com.niknik.woody.utils.wunit;

import com.niknik.woody.utils.ExCreateAndLogFor;
import org.hamcrest.Matcher;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

import static com.niknik.woody.utils.wunit.junit.matcher.IsEx.*;
import static com.niknik.woody.utils.wunit.junit.runner.WUnitRunner.getContext;
import static org.junit.Assert.*;

// TODO: our own testing framework
public class WUnit {

  private WUnit() {
    throw ExCreateAndLogFor.methodBlocked();
  }

  public static void assertThatSize(Collection collection, Matcher<Integer> matcher) {
    assertNotNull(collection);
    assertThat(collection.size(), matcher);
  }

  public static void assertThatCannotInstantiateSubjectClass() {
    getContext().getLogMonitor().expectAssertionThrow("Method is blocked.");

    try {
      Constructor constr = getContext().getTestInfo().getSubjectClass().getDeclaredConstructor();
      constr.setAccessible(true);
      constr.newInstance();
      fail();
    }
    catch (InvocationTargetException ex) {
      assertThat(ex.getCause(), isAssertionEx("Method is blocked."));
    }
    catch (Exception ex) {
      throw new AssertionError(ex);
    }
  }

  public static void expectThrowArgEx(String msg) {
    getContext().getLogMonitor().expectArgThrow(msg);
    getContext().getExMonitor().expect(isArgEx(msg));
  }

  public static void expectThrowStateEx(String msg) {
    getContext().getLogMonitor().expectStateThrow(msg);
    getContext().getExMonitor().expect(isStateEx(msg));
  }
}
